/*******************************************************************************
 * Copyright 2010 DemandSpot
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.demandspot.client.table;

import java.util.Comparator;
import java.util.Date;

public class DefaultTableComparator implements Comparator {

	public int compare(Object a, Object b) {		
		if (a instanceof Number && b instanceof Number) {
			double A = ((Number) a).doubleValue();
			double B = ((Number) b).doubleValue();
			return (A < B) ? -1 : (A == B) ? 0 : 1; 
		}
		else if (a instanceof Date && b instanceof Date) {
			Date A = (Date) a;
			Date B = (Date) b;
			return A.before(B) ? -1 : (A.after(B)) ? 1 : 0;
		} 
		else if (a instanceof Comparable && b instanceof Comparable) {
			Comparable A = (Comparable) a;
			Comparable B = (Comparable) b;
			return A.compareTo(B);	 
		}
		else {
			String A = a.toString();
			String B = b.toString();
			return A.compareTo(B);
		}
	}

}
